package com.kailong.servlet.student;

import com.kailong.dao.BaseDao;
import com.kailong.dao.student.SelectStudentDao;
import com.kailong.pojo.Student;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/studentLogin")
public class StudentLoginServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String sno = request.getParameter("userid");
        String pwd = request.getParameter("password");
        int count = SelectStudentDao.selectBySno(sno,pwd);
        String Usercode = request.getParameter("Usercode");
        String code = (String)request.getSession().getAttribute("piccode");
        PrintWriter out = response.getWriter();
        if (!Usercode.equals(code)){
            out.print("<script>");
            out.print("alert('验证码错误！请重新输入！');window.location.href=\"http://localhost:8080/Library/\"");
            out.print("</script>");
        } else if (count>0){
            HttpSession session = request.getSession();
            Student student = SelectStudentDao.selectAdmin(sno,pwd);
            request.setAttribute("sname",student.getSname());
            request.setAttribute("sclass",student.getSclass());
            request.setAttribute("sname",student.getSname());
            request.setAttribute("sno",student.getSno());
            session.setAttribute("sclass",student.getSclass());
            session.setAttribute("sno",student.getSno());
            session.setAttribute("sname",student.getSname());
            session.setAttribute("isLogin","1");
            session.setAttribute("sclass",student.getSclass());
            response.sendRedirect("student/studentManage.jsp");
        }else{
            out.write("<script>");
            out.write("alert('用户登录失败！');");
            out.write("location.href='http://localhost:8080/Library/';");
            out.write("</script>");
            out.close();
        }

    }
}
